<?php

class User{
	
	private $id;
	private $username;
	private $defaultProfile;
	private $currentProfile;
	private $profiles = array();
	private $registry;
	private $banned;
	private $admin = 0;
	private $active = 0;
	private $valid = false;
	
	public function __construct( Registry $registry, $id=0, $username='', $password='' )
	{
		$this->registry = $registry;
		
		if( $id == 0 && $username != '' && $password != '' )
		{
			$user = $this->registry->getObject('db')->sanitizeData( $username );
			$hash = md5( $password );
			$sql = "SELECT * FROM users WHERE username='{$user}' AND password_hash='{$hash}' AND deleted=0";
			//echo $sql;
			$this->registry->getObject('db')->executeQuery( $sql );
			if( $this->registry->getObject('db')->numRows() == 1 )
			{
				$data = $this->registry->getObject('db')->getRows();
				$this->id = $data['uid'];
				$this->username = $data['username'];
				$this->active = $data['active'];
				$this->banned = $data['banned'];
				$this->admin = $data['admin'];
				//$this->email = $data['email'];
				$this->pwd_reset_key = $data['reset_key'];
				$this->valid = true;
			}
		}
		elseif( $id > 0 )
		{
			$id = intval( $id );
			$sql = "SELECT * FROM users WHERE uid ='{$id}' AND deleted=0";
			$this->registry->getObject('db')->executeQuery( $sql );
			if( $this->registry->getObject('db')->numRows() == 1 )
			{
				$data = $this->registry->getObject('db')->getRows();
				$this->id = $data['uid'];
				$this->username = $data['username'];
				$this->active = $data['active'];
				$this->banned = $data['banned'];
				$this->admin = $data['admin'];
				//$this->email = $data['email'];
				$this->pwd_reset_key = $data['reset_key'];
				$this->valid = true;
			}
		}
		
		if ($this->valid)
		{// Load available profiles
			$this->loadProfiles();
		}
	}
	
	public function loadProfiles()
	{
		unset($this->profiles);
		$sql = "SELECT name, user_id, profile_id, profile_name, is_default, photo from profile where user_id = ".$this->id;

		$success = $this->registry->getObject('db')->executeQuery( $sql );
			
		require_once( FRAMEWORK_PATH . 'models/profile.php');
			
		$defaultProfInfo = array();
			
		while( $row = $this->registry->getObject('db')->getRows() )
		{				
			if ($row['profile_name'] == '')
			{
				
				$row['profile_name'] = "Default";
			}
		
			if ($row['photo'] == '')
				$row['photo'] = "resources/images/default/profile.jpg";
				
			$this->profiles[] = $row;
			if ($row['is_default'] == 1)
			{
				$defaultProfInfo = $row;
			}
		}

		$this->defaultProfile = new Profile($this->registry, $this->id, $defaultProfInfo['profile_id']);
		$this->defaultProfile->setPhoto($defaultProfInfo['photo']);
		$this->currentProfile = $this->defaultProfile;
			
		if (isset($_SESSION['sn_current_profile_id']) && intval( $_SESSION['sn_current_profile_id'] ) > 0)
		{
			$profileid = $_SESSION['sn_current_profile_id'];
			$sql = "SELECT profile_id, profile_name, photo from profile where user_id = '$this->id' AND profile_id = '$profileid'";
			$success = $this->registry->getObject('db')->executeQuery( $sql );
			$data = $this->registry->getObject('db')->getRows();


			$this->currentProfile = new Profile($this->registry, $this->id, $_SESSION['sn_current_profile_id'] );
			$this->currentProfile->setPhoto($data['photo']);
		}
	}
	
	public function getUserID()
	{
		return $this->id;
	}
	
	public function getUsername()
	{
		return $this->username;
	}
	
	public function resetPassword( $password )
	{
		
	}
	
	public function getDefaultProfile()
	{
		return $this->defaultProfile ;
	}
	
	public function setDefaultProfile($profile)
	{
		$this->defaultProfile = $profile;
	}
	
	public function getCurrentProfile()
	{
		return $this->currentProfile;
	}
	
	public function setCurrentProfile($profile)
	{
		$this->currentProfile = $profile;
	}
	
	public function getProfiles()
	{
		return  $this->profiles;
	}
	
	public function getEmail()
	{
		return $this->email;
	}
	
	public function isActive()
	{
		return ( $this->active == 1 ) ? true : false;
	}
	
	public function isAdmin()
	{
		return ( $this->admin == 1 ) ? true : false;
	}
	
	public function isBanned()
	{
		return ( $this->banned == 1 ) ? true : false;
	}
	
	public function isValid()
	{
		return $this->valid;
	}
	
}

?>